﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Wyse.Server.Common
{
    /// <summary>
    /// Represents an I/O stream built around a circular buffer
    /// </summary>
    public class CircularBufferStream : Stream
    {
        /// <summary>
        /// Default ctor
        /// </summary>
        public CircularBufferStream(int capacityHint = 1024)
        {
            mBuffer = new CircularBuffer<byte>(capacityHint);
        }

        /// <summary>
        /// Determines if the stream supports reading
        /// </summary>
        public override bool CanRead
        {
            get { return true; }
        }

        /// <summary>
        /// Determines if the stream supports seeking
        /// </summary>
        public override bool CanSeek
        {
            get { return false; }
        }

        /// <summary>
        /// Determines if the stream supports writing
        /// </summary>
        public override bool CanWrite
        {
            get { return true; }
        }

        /// <summary>
        /// Flushes the stream's buffers
        /// </summary>
        public override void Flush()
        {
        }

        /// <summary>
        /// Gets the stream's length
        /// </summary>
        public override long Length
        {
            get { return mBuffer.Size; }
        }

        /// <summary>
        /// Gets or sets the stream pointer position - not supported
        /// </summary>
        public override long Position
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        /// <summary>
        /// Reads a number of bytes from the stream
        /// </summary>
        public override int Read(byte[] buffer, int offset, int count)
        {
            count = Math.Min(mBuffer.Size, count);
            mBuffer.Pop(buffer, offset, count);

            return count;
        }

        /// <summary>
        /// Reads a single byte from the stream
        /// </summary>
        /// <returns></returns>
        public override int ReadByte()
        {
            if (mBuffer.Size == 0)
                return -1;
            return mBuffer.Pop();
        }

        /// <summary>
        /// Changes the position of the stream pointer - not supported
        /// </summary>
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Changes the length of the stream - not supported
        /// </summary>
        /// <param name="value"></param>
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Writes a number of bytes to the stream
        /// </summary>
        public override void Write(byte[] buffer, int offset, int count)
        {
            mBuffer.Push(buffer, offset, count);
        }

        /// <summary>
        /// Writes a single byte to the stream
        /// </summary>
        public override void WriteByte(byte value)
        {
            mBuffer.Push(value);
        }

        private CircularBuffer<byte> mBuffer;
    }
}
